//
//  AXFDataModel.m
//  AXFEightGroup
//
//  Created by ya on 12/22/16.
//  Copyright © 2016 ya. All rights reserved.
//

#import "AXFDataModel.h"
#import <objc/message.h>

@interface AXFDataModel ()

@end

@implementation AXFDataModel

#pragma mark - 配置

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    if (self) {

        NSMutableDictionary *dictM = [NSMutableDictionary new];
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
                [dictM setValue:[NSString stringWithFormat:@"%@", obj] forKey:key];
            }];
        }

        [self setValuesForKeysWithDictionary:dictM];
        self.iD         = dictM[@"id"];
        self.imgURL     = dictM[@"img"];
        self.aNewStatus = dictM[@"newStatus"];
    }
    return self;
}

//+ (instancetype)focuModelWithDictinary:(NSDictionary *)dict {
//    return [[[self class] alloc] initWithDictinary:dict];
//}
//+ (instancetype)iconModelWithDictinary:(NSDictionary *)dict {
//    return [[[self class] alloc] initWithDictinary:dict];
//}
//+ (instancetype)activitieModelWithDictinary:(NSDictionary *)dict {
//    return [[[self class] alloc] initWithDictinary:dict];
//}
//+ (instancetype)firstSellModelWithDictinary:(NSDictionary *)dict; {
//    return [[[self class] alloc] initWithDictinary:dict];
//}
//+ (instancetype)orderModelWithDictionary:(NSDictionary *)dict {
//    return [[[self class] alloc] initWithDictinary:dict];
//}
//+ (instancetype)adressModelWithDictionary:(NSDictionary *)dict {
//    return [[[self class] alloc] initWithDictinary:dict];
//}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    if ([key isEqualToString:@"id"]  ||
        [key isEqualToString:@"img"] ||
        [key isEqualToString:@"newStatus"]) {
        return;
    }
    NSLog(@"UndefinedKey: %@, %s", key, __func__);
}

- (void)setValue:(id)value forKey:(NSString *)key {
    [super setValue:value forKey:key];
    if ([key isEqual:@"description"]) {
        
        self.desc = value;
//        key = @"desc";
        
    }
    
}

///// order_goods
- (void)setOrder_goods:(NSArray<NSArray *> *)order_goods {
    _order_goods = [self modelWithArray:order_goods];
}

///// status_timeline
- (void)setStatus_timeline:(NSArray<NSDictionary *> *)status_timeline {
    _status_timeline = [self modelWithArray:status_timeline];
}

//// fee_list
- (void)setFee_list:(NSArray<NSDictionary *> *)fee_list {
    _fee_list = [self modelWithArray:fee_list];
}

//// buttons
- (void)setButtons:(NSArray<NSDictionary *> *)buttons {
    _buttons = [self modelWithArray:buttons];
}

//// detail_buttons
- (void)setDetail_buttons:(NSArray<NSDictionary *> *)detail_buttons {
    _detail_buttons = [self modelWithArray:detail_buttons];
}

/// focus
- (void)setFocus:(NSArray<NSDictionary *> *)focus {
    _focus = [self modelWithArray:focus];
}

/// titles
- (void)setTitles:(NSArray<NSString *> *)titles {
    NSMutableArray<NSString *> *strings = [NSMutableArray new];
    [titles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [strings addObject:obj];
    }];
    _titles = strings.copy;
}

/// texts
- (void)setTexts:(NSArray<NSString *> *)texts {

    if ([texts isKindOfClass:[NSString class]]) {
        _texts = @[texts];
        return;
    }

    NSMutableArray<NSString *> *strings = [NSMutableArray new];
    [texts enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [strings addObject:obj];
    }];
    _texts = strings.copy;
}


- (NSArray<AXFDataModel *> *)modelWithArray:(NSArray<NSDictionary *> *)array {


    if ([array isKindOfClass:[NSArray class]]) {
        NSMutableArray<AXFDataModel *> *models = [NSMutableArray new];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull dict, NSUInteger idx, BOOL * _Nonnull stop) {
            AXFDataModel *model = [[[self class] alloc] initWithDictionary:dict];
            [models addObject:model];
        }];
        return models.copy;
    }

    return @[array];

}

- (CGFloat)productTotalPrice {
    return self.partner_price.floatValue * self.productAmount;
}

#pragma mark -


#pragma mark - 归档


- (void)encodeWithCoder:(NSCoder *)aCoder {

    unsigned int outCout = 0;
    Ivar *ivarList = class_copyIvarList([self class], &outCout);
    if (ivarList != NULL && outCout > 0) {
        for (int i = 0; i < outCout; i ++) {
            Ivar ivar = ivarList[i];
            const char *key = ivar_getName(ivar);
            id value = object_getIvar(self, ivar);
            if (value != nil) {
                [aCoder encodeObject:value forKey:[NSString stringWithUTF8String:key]];
            }
        }
    }
    free(ivarList);

}


- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
    unsigned int outCout = 0;
    Ivar *ivarList = class_copyIvarList([self class], &outCout);
    if (ivarList != NULL && outCout > 0) {
        for (int i = 0; i < outCout; i ++) {
            Ivar ivar = ivarList[i];
            const char *key = ivar_getName(ivar);
            NSString *keyStr = [NSString stringWithUTF8String:key];
            if ([aDecoder containsValueForKey:keyStr]) {
                id value = [aDecoder decodeObjectForKey:keyStr];
                object_setIvar(self, ivar, value);
            }
        }
    }
    free(ivarList);

    return self;
}

#pragma mark -


#pragma mark - 重写 description


- (NSString *)description
{
    NSMutableString *description = [NSMutableString string];
    [description appendFormat:@"\n"];
    unsigned int outCout = 0;
    Ivar *ivarList = class_copyIvarList([self class], &outCout);
    if (ivarList != NULL && outCout > 0) {
        for (int i = 0; i < outCout; i ++) {
            Ivar ivar = ivarList[i];
            const char *key = ivar_getName(ivar);
            const char *type = ivar_getTypeEncoding(ivar);
            if ( !strcmp(type, @encode(NSInteger)) ) {
                continue;
            }
            if ( !strcmp(type, @encode(CGFloat)) ) {
                continue;
            }
            id value = object_getIvar(self, ivar);
            if (value) {
                NSString *keyStr = [[NSString alloc] initWithCString:key encoding:NSUTF8StringEncoding];
                [description appendFormat:@"%@ = %@\n", keyStr, value];
            }
        }
    }
    free(ivarList);
    [description appendFormat:@"\n"];
    return description;
}


#pragma mark -

@end






















